package com.hqyj.freemovie.front.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//生成验证码图片的接口
@Controller
@RequestMapping("/commons")
public class CommonsController extends BaseController {


    @RequestMapping("/randomcode")
    public void generateRandomCodeImage(HttpServletRequest request, HttpServletResponse response,String token){


        try{
            //定义图形验证码的长、宽、验证码字符数、干扰线宽度
            ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(170, 50, 4, 4);
            //ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
            //图形验证码写出，可以写出到文件，也可以写出到流
            String randomCode = captcha.getCode();
            System.out.println("生成的验证码是：" + randomCode);
            //把验证码怎么存在到application中。
            //这个键应该是啥？
            //request.getServletContext().setAttribute(token,randomCode); //Application ==>redis缓存
            redisUtil.set(token,randomCode,5);
            //System.out.println();
            //captcha.write("d:/shear.png");
            //验证图形验证码的有效性，返回boolean值
            //captcha.verify("1234");
            captcha.write(response.getOutputStream()); //把验证码输出到浏览器上。画到浏览器上。
        }catch(Exception ex){
            ex.printStackTrace();

        }
    }
}
